#include "ws.client.h"

#ifdef DEBUG
#include <iostream>
#endif

TOB::WS::Data::Data(int8* buffer, int32 length) {
  _length = length;
  _buffer = new int8[length + 1];
  memcpy (_buffer, buffer, length);
  _buffer[_length] = 0;
  _data = new std::string(_buffer);
#ifdef DEBUG
  std::cout << "created data: " << this << std::endl;
#endif
}

TOB::WS::Data::Data(std::string buffer) {
  _length = buffer.length();
  _buffer = new int8[_length + 1];
  memcpy (_buffer, buffer.c_str(), _length);
  _buffer[_length] = 0;
  _data = new std::string(_buffer);
#ifdef DEBUG
  std::cout << "created data: " << this << std::endl;
#endif
}

TOB::WS::Data::Data() {
  _length = 0;
  _buffer = NULL;
  _data = NULL;
#ifdef DEBUG
  std::cout << "created data: " << this << std::endl;
#endif
}

TOB::WS::Data::~Data() {
  if (_buffer != NULL) {
    delete[] _buffer;
  }
  if (_data != NULL) {
    delete _data;
  }
#ifdef DEBUG
  std::cout << "deleted data: " << this << std::endl;
#endif
}

int32& TOB::WS::Data::getLength() {
  return _length;
}

int8* TOB::WS::Data::getBuffer() {
  return _buffer;
}

std::string& TOB::WS::Data::getData() {
  return *_data;
}

int8 TOB::WS::Data::operator [](int32 index) {
  return _buffer[index];
}

TOB::WS::Data& TOB::WS::Data::operator =(const TOB::WS::Data& right) {
  if (this == &right) {
    return *this;
  }
  
  if (_buffer != NULL) {
    delete[] _buffer;
  }
  if (_data != NULL) {
    delete _data;
  }
  
  _length = right._length;
  _buffer = new int8[_length + 1];
  memcpy (_buffer, right._buffer, _length);
  _buffer[_length] = 0;
  _data = new std::string(_buffer);
  
  return *this;
}
